package com.monco.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.monco.entity.Rubbish;
import com.monco.mapper.RubbishMapper;
import com.monco.service.IRubbishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author com/monco
 * @since 2020-04-19
 */
@Service
public class RubbishServiceImpl extends ServiceImpl<RubbishMapper, Rubbish> implements IRubbishService {

    @Resource
    private RubbishMapper rubbishMapper;

    @Override
    public List<Rubbish> getRubbishList(Rubbish rubbish) {
        return rubbishMapper.selectList(getWrapper(rubbish));
    }

    @Override
    public IPage<Rubbish> getRubbishPage(IPage<Rubbish> page, Rubbish rubbish) {
        return rubbishMapper.selectPage(page, getWrapper(rubbish));
    }

    private QueryWrapper<Rubbish> getWrapper(Rubbish rubbish) {
        QueryWrapper<Rubbish> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(rubbish.getRubbishType())) {
            queryWrapper.eq("rubbish_type", rubbish.getRubbishType());
        }
        if (StringUtils.isNotBlank(rubbish.getRubbishName())) {
            queryWrapper.like("rubbish_name", rubbish.getRubbishName());
        }
        queryWrapper.orderByDesc("id");
        return queryWrapper;
    }
}
